package com.situ.system.department.service;

import java.util.List;

import com.situ.system.department.dao.DepartmentDao;
import com.situ.system.department.model.DepartmentModel;

public class DepartmentService {

	private DepartmentDao dao = new DepartmentDao();

	public String insert(DepartmentModel model) {
		DepartmentModel model2 = new DepartmentModel(model.getId());
		if (selectModel(model2) == null) {
			return dao.insert(model);
		} else {
			return "-1";
		}
	}

	public String delete(DepartmentModel model) {
		return dao.delete(model);
	}

	public String update(DepartmentModel model) {
		return dao.update(model);
	}

	public DepartmentModel selectModel(DepartmentModel model) {
		DepartmentModel m0 = new DepartmentModel();
		m0.setId(model.getId());
		List<DepartmentModel> list = dao.select(m0);
		return list == null || list.isEmpty() ? null : list.get(0);
	}

	public List<DepartmentModel> selectList(DepartmentModel model) {
		String code = model.getId();
		if (code == null)
			code = "";
		model.setId("%" + code + "%");
		String name = model.getName();
		if (name == null)
			name = "";
		model.setName("%" + name + "%");
		return dao.select(model);
	}

	public Integer selectCount(DepartmentModel model) {
		DepartmentModel m0 = new DepartmentModel();
		String code = model.getId();
		if (code == null)
			code = "";
		m0.setId("%" + code + "%");
		String name = model.getName();
		if (name == null)
			name = "";
		m0.setName("%" + name + "%");
		return dao.selectCount(m0);
	}
}
